package models

import (
	"fmt"
	"net"
	"runtime/debug"
	"strconv"
	"strings"
	"time"
)

func 客户端验证() {
	login, err := net.Listen("tcp", ":"+strconv.Itoa(Config.Server.Port))
	if err != nil {
		panic(err)
	}
	m_启动时间 := time.Now().Format("2006-01-02 15:04:05")
	for {
		tcpConn, err := login.Accept()
		if err != nil {
			fmt.Println("用户接收程序退出")
			return
		}
		go func(tcp net.Conn) {
			defer func() {
				if err := recover(); err != nil {
					debug.PrintStack()
					fmt.Println(err)
				}
				tcp.Close()
			}()
			rxbuff := make([]byte, 4096)
			if rxlen, err := tcp.Read(rxbuff); err != nil {
				return
			} else {
				s := string(rxbuff[:rxlen])
				sp := strings.Split(s, "\n")
				switch sp[0] {
				case "login":
					if len(sp) != 3 {
						tcp.Write([]byte(UTF8ToGBK("false\n用户名或密码错")))
						return
					}
					//在游戏中查找这个账号
					player := &Struct用户信息{}
					if ok, _ := db.Where("用户名=?", sp[1]).Where("密码=?", sp[2]).Get(player); ok == false {
						tcp.Write([]byte(UTF8ToGBK("false\n用户名或密码错")))
					} else {
						s += strconv.FormatInt(time.Now().Unix(), 10)
						pass := make([]byte, 32)
						//先得到一个全部有值的缓冲区
						for i := 0; i < 32; i++ {
							pass[i] = s[i%len(s)]
						}
						tw := time.Now().Unix()
						key := make([]byte, 8)
						//再取得一个KEY缓冲区
						for i := 0; i < 8; i++ {
							key[i] = byte(tw)
							tw >>= 8
						}
						for i := 0; i < 8; i++ {
							for ii := 0; ii < 32; ii++ {
								pass[ii] ^= key[i]
							}
						}
						加密串 := Byte2String(pass)
						加密串 = strings.Replace(加密串, " ", "", -1)
						if _, err := db.Table(player).Where("id=?", player.Id).Update(map[string]interface{}{"加密串": 加密串}); err != nil {
							panic(err)
						}
						tcp.Write([]byte("true\n" + 加密串))
					}
				case "reguser":
					if len(sp) < 3 {
						tcp.Write([]byte(UTF8ToGBK("false\n注册结构不正确")))
						return
					}
					if (len(sp[1]) < 6) || (len(sp[1]) > 16) {
						tcp.Write([]byte(UTF8ToGBK("false\n用户名密码必须是6-16位")))
						return
					}

					player := &Struct用户信息{}
					player.V用户名 = sp[1]
					player.V密码 = sp[2]
					if _, err := db.Insert(player); err != nil {
						tcp.Write([]byte(UTF8ToGBK("false\n" + err.Error())))
					} else {
						tcp.Write([]byte(UTF8ToGBK("true\n" + GetConfig("系统", "注册欢迎语", "欢迎您的到来!"))))
					}
				case "postmsg":
					if len(sp) != 3 {
						tcp.Write([]byte(UTF8ToGBK("false\n注册结构不正确")))
						return
					}

					msg := &Struct问题反馈{}
					msg.V添加日期 = time.Now()
					msg.V标题 = GBKToUTF8(sp[1])
					msg.V内容 = GBKToUTF8(sp[2])
					if _, err := db.Insert(msg); err != nil {
						tcp.Write([]byte(UTF8ToGBK("false\n" + err.Error())))
					} else {
						tcp.Write([]byte(UTF8ToGBK("true\n谢谢")))
					}
				case "getmsg":
					sends := "true\n"
					sends += Ver + " 启动时间:" + m_启动时间 + "\n"
					sends += "在线人数:"
					usercnt := 0
					m_RunTime.Range(func(key, value interface{}) bool {
						usercnt++
						return true
					})
					sends += strconv.Itoa(usercnt)
					sends += "\n"
					sends += GetConfig("系统", "服务器介绍", "未添加介绍") + "\n"
					tcp.Write([]byte(UTF8ToGBK(sends)))
				case "SUPERCMD":
					if len(sp) < 3 {
						tcp.Write([]byte(UTF8ToGBK("无效超级指令:" + s)))
						break
					}
					if sp[1] != Config.Debug.Password {
						tcp.Write([]byte(UTF8ToGBK("秘钥错")))
						break
					}
					sp = sp[2:]
					var rts []*RunTime
					if sp[0] == "all" {
						m_RunTime.Range(func(key, value interface{}) bool {
							rts = append(rts, value.(*RunTime))
							return true
						})
					} else {
						m_RunTime.Range(func(key, value interface{}) bool {
							if value.(*RunTime).V角色.V名字 == sp[0] {
								rts = append(rts, value.(*RunTime))
								return false
							}
							return true
						})
					}
					cmds := strings.Split(sp[1], ",")
					var args [][]byte
					cmd := String2Byte(cmds[0])[0]
					for i := 1; i < len(cmds); i++ {
						args = append(args, String2Byte(cmds[i]))
					}
					for _, rt := range rts {
						rt.AddHeadSend10(cmd, args)
					}
					tcp.Write([]byte(UTF8ToGBK("已向角色发送数据:" + strconv.Itoa(len(rts)))))
				case "ADDITEM":
					if len(sp) != 3 {
						tcp.Write([]byte(UTF8ToGBK("无效指令:" + s)))
						break
					}
					if sp[1] != Config.Debug.Password {
						tcp.Write([]byte(UTF8ToGBK("秘钥错")))
						break
					}
					bys := String2Byte(sp[2])
					if len(bys) == 0 {
						tcp.Write([]byte(UTF8ToGBK("无效数据:" + sp[2])))
					} else {
						buff := CreateBuff(len(bys))
						buff.AddBytes(bys)
						tcp.Write([]byte(UTF8ToGBK("返回结果:" + 物品_解析并添加物品(buff))))
					}
				default:
					tcp.Write([]byte(UTF8ToGBK("无效指令")))
					return
				}
			}
		}(tcpConn)
	}
}
